package com.efounder.form.comp;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.widget.LinearLayout;

import com.efounder.builder.base.data.DataSetEvent;
import com.efounder.builder.base.data.DataSetListener;
import com.efounder.builder.base.data.EFDataSet;
import com.efounder.builder.base.data.EFRowSet;
import com.efounder.form.DMColComponent;
import com.efounder.form.DMComponent;
import com.efounder.form.DataSetComponent;
import com.efounder.form.base.IComponent;
import com.efounder.form.model.JSONFormModel;
import com.efounder.form.util.FormCompUtil;
import com.efounder.frame.ViewSize;
import com.efounder.mobilecomps.R;
import com.efounder.util.AppContext;
import com.mixiaoxiao.smoothcompoundbutton.SmoothRadioGroup;

import java.util.ArrayList;
import java.util.List;
/**
 * Created by lch on 16/9/2.
 * 单选框--方形
 */
public class FormRadioGroup extends SmoothRadioGroup implements IComponent,IUIComponent,DMComponent,DMColComponent,DataSetListener {
	private String[] dataArray;
	private List<FormRadioButton> efRadioButtons;
	private String currentSelectedItem="";
	private IUIComponent parentComp;
	//自己更改mainrowset时，无需触发datasetchange事件。
	private boolean isSelfDataSetChange = false;

	public FormRadioGroup() {
		super(AppContext.getCurrentActivity());
	}
	public FormRadioGroup(Context context) {
		super(AppContext.getCurrentActivity());
	}
	public FormRadioGroup(Context context, AttributeSet attrs) {
		super(AppContext.getCurrentActivity(), attrs);
	}

	/**
	 * 初始化样式
	 */
	@Override
	public void creationComplete() {
		//设置每个radiobutton的大小
		for(int i=0;i<efRadioButtons.size();i++) {
			FormRadioButton efRadioButton = efRadioButtons.get(i);
			if (layoutType.equals("horizontal")) {
				efRadioButton.setWidth(this.getViewSize().getWidth()/ efRadioButtons.size());
				efRadioButton.setHeight(this.getViewSize().getHeight());
				Log.i("FormRadioGrop----height",((this.getViewSize().getHeight())+""));
			} else if (layoutType.equals("vertical")) {
				efRadioButton.setWidth(this.getViewSize().getWidth());
				efRadioButton.setHeight(this.getViewSize().getHeight() / efRadioButtons.size());
			} else {
			}
			efRadioButton.setClickable(this.getEditable());
			efRadioButton.setCanEdit(this.getEditable());

		}

		//设置默认方向，这个可以根据表单中的值设置
		this.setOrientation(LinearLayout.HORIZONTAL);

		this.setBackgroundResource(android.R.color.white);

		this.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(SmoothRadioGroup smoothRadioGroup, int i) {
				//TODO 更改Change dataset。
				if(mainRowSet!=null) {
					isSelfDataSetChange = true;
					mainRowSet.putString(dataSetColID, dataArray[i]);
					currentSelectedItem = dataArray[i];
				}
			}
		});

	}

	/**
	 * datasetchange事件
	 * @param dataSet
     */
	public void dataChanged(EFDataSet dataSet) {
		if(!isSelfDataSetChange) {
			mainRowSet = dataSet.getCurrentRowSet();
			String key = "";
			if (mainRowSet != null) {
				key = mainRowSet.getString(dataSetColID, "");
				if (!key.equals("") && !currentSelectedItem.equals(key)) {
					getFormRadioButtonByID(key).setChecked(true);
				}
			}
		}
		isSelfDataSetChange = false;

	}

	@Override
	public DataSetComponent getDataSetComponent() {
		return dataSetComponent;
	}

	@Override
	public void setDataSetComponent(DataSetComponent dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

	public void setDataSetComponent(JSONFormModel dsc) {
		if (dataSetComponent != null)
			dataSetComponent.removeDMComponent(this);
		dataSetComponent = dsc;
		if (dataSetComponent != null)
			dataSetComponent.insertDMComponent(this);
	}

	@Override
	public void setLayout() {
		FormCompUtil.setLayoutSize(this);
		this.setGravity(FormCompUtil.getLayoutGravity(horizontalAlign,verticalAlign));
		this.setGravity(Gravity.CENTER);
	}

	/**
	 * 根据id得到子radiobutton
	 * @param id
	 * @return
     */
	private FormRadioButton getFormRadioButtonByID(String id){
		FormRadioButton formRadioButton;
		for(int i=0;i<efRadioButtons.size();i++){
			formRadioButton = efRadioButtons.get(i);
			if(formRadioButton.getID().equals(id))
				return  formRadioButton;
		}
		return  null;
	}

	public String getHorizontalAlign() {
		return horizontalAlign;
	}

	public void setHorizontalAlign(String horizontalAlign) {
		this.horizontalAlign = horizontalAlign;
	}

	public String getVerticalAlign() {
		return verticalAlign;
	}

	public void setVerticalAlign(String verticalAlign) {
		this.verticalAlign = verticalAlign;
	}

	@Override
	public void setDataSetID(String dataSetID) {
		this.dataSetID = dataSetID;
	}

	@Override
	public String getDataSetColID() {
		return dataSetColID;
	}

	@Override
	public void setDataSetColID(String dataSetColID) {
		this.dataSetColID = dataSetColID;
	}

	@Override
	public String getDataSetNameColID() {
		return null;
	}

	@Override
	public void setDataSetNameColID(String value) {

	}

	@Override
	public String getInternalDataSetID() {
		return null;
	}

	@Override
	public void setInternalDataSetID(String dataSetID) {

	}

	@Override
	public void setViewDataSetID(String viewDataSetID) {

	}

	@Override
	public String getViewDataSetID() {
		return null;
	}

	@Override
	public void setViewDataSetColID(String viewDataSetColID) {

	}

	@Override
	public String getViewDataSetColID() {
		return null;
	}

	@Override
	public void setValueDataSetColID(String valueDataSetColID) {

	}

	@Override
	public String getValueDataSetColID() {
		return null;
	}

	@Override
	public Boolean getIsUserInternalDataSetID() {
		return null;
	}

	@Override
	public void setIsUserInternalDataSetID(Boolean v) {

	}

	@Override
	public String getIdentifier() {
		return null;
	}

	@Override
	public Object getPropertyMap() {
		return null;
	}

	@Override
	public void setPropertyMap(Object value) {

	}

	@Override
	public Object getProperty(String key, Object defVal) {
		return null;
	}

	@Override
	public void setProperty(String key, Object value) {

	}

	@Override
	public int getNumberPrecision() {
		return 0;
	}

	@Override
	public void seNumberPrecision(int value) {

	}

	@Override
	public String getNumberFormat() {
		return null;
	}

	@Override
	public void setNumberFormat(String numberFormat) {

	}

	@Override
	public String getColumnType() {
		return null;
	}

	@Override
	public void setColumnType(String value) {

	}

	@Override
	public String getEditorType() {
		return null;
	}

	@Override
	public void setEditorType(String value) {

	}
	@Override
	public String getDateFormat() {
		return null;
	}

	@Override
	public void setDateFormat(String dateFormat) {

	}

	@Override
	public String getFormulaOne() {
		return null;
	}

	@Override
	public void setFormulaOne(String formulaOne) {

	}



	@Override
	public String getFkeyID() {
		return null;
	}

	@Override
	public void setFkeyID(String fkey) {

	}

	@Override
	public String getRlglID() {
		return null;
	}

	@Override
	public void setRlglID(String rlglID) {

	}

	@Override
	public String getDataSetID() {
		return dataSetID;
	}

	@Override
	public void setDataSet(EFDataSet dataSet) {
		if ( this.dataSet != dataSet ) {
			if ( this.dataSet != null ) this.dataSet.removeDataSetListener(this);
			this.dataSet = dataSet;
			if ( this.dataSet != null ) this.dataSet.addDataSetListener(this);
			this.dataChanged(dataSet);
		}

	}

	@Override
	public EFDataSet getDataSet() {
		return dataSet;
	}

	@Override
	public EFRowSet getMainRowSet() {
		return mainRowSet;
	}

	@Override
	public void dataSetChanged(DataSetEvent event) {
		dataChanged(event.getDataSet());
	}

	@Override
	public String getID() {
		return id;
	}

	@Override
	public void setID(String id) {
		this.id = id;
	}

	@Override
	public int getGap() {
		return gap;
	}

	@Override
	public void setGap(int gap) {
		this.gap = gap;
	}


	@Override
	public void setPercentWidth(int percentW) {
		percentWidth = percentW;
	}

	@Override
	public int getPercentHeight() {
		return percentHeight;
	}

	@Override
	public void setPercentHeight(int percentH) {
		percentHeight = percentH;
	}

	@Override
	public ViewSize getViewSize() {
		return size;
	}

	@Override
	public void setViewSize(ViewSize size) {
		this.size = size;
	}

	@Override
	public String getEditMask() {
		return null;
	}

	@Override
	public void setEditMask(String editMask) {

		String[] datamodelArray = editMask.split(";");
		if(datamodelArray.length>0){
			dataArray = new String[datamodelArray.length];
			efRadioButtons = new ArrayList<>();
			for(int i = 0;i<datamodelArray.length;i++){
				FormRadioButton efRadioButton = new FormRadioButton();
				this.addView(efRadioButton);
				efRadioButtons.add(efRadioButton);
				String key =  datamodelArray[i].substring(0,datamodelArray[i].indexOf(":"));
				String value = datamodelArray[i].substring(datamodelArray[i].indexOf(":")+1,datamodelArray[i].length());
				//String value = datamodelArray[i];
				efRadioButton.setID(key);
				efRadioButton.setId(i);
				efRadioButton.setText(value);
				dataArray[i] = key;
				efRadioButton.setEnabled(true);

				efRadioButton.setClickable(true);
				efRadioButton.setGravity(Gravity.CENTER_VERTICAL);
				efRadioButton.setTextSize(fontSize);
				efRadioButton.setEllipsize(TextUtils.TruncateAt.END);
				//this.setSingleLine(true);
				//由于radiobutton包含动画，所以我在这里设置了paddingleft，不然的话，动画会显示不全。
				efRadioButton.setPadding(20,0,0,0);
				efRadioButton.setBackgroundResource(android.R.color.white);

				efRadioButton.setTextColor(getResources().getColor(R.color.black));
			}
		}

	}
	@Override
	public void setCompWidth(int width) {
		this.width = width;
	}

	@Override
	public int getCompWidth(){
		return this.width;
	}

	@Override
	public void setCompHeight(int height) {
		this.height = height;
	}

	@Override
	public int getCompHeight() {
		return this.height;
	}

	private String id;
	private float x = 0;
	private float y = 0;
	private int width;
	private int height;
	private Boolean visible;
	private int tabIndex = 0;
	private int fontColor = 0;
	private int fontSize = 14;
   //默认是不可编辑的
	private Boolean editable = true;
	private String editMask;
	/**
	 * 布局类型：垂直vertical、水平horizontal
	 */
	private String layoutType = "horizontal";
	private int requestColCount;
	private double horizontalGap;
	private double verticalGap;
	private int percentWidth;
	private int percentHeight;
	private int gap = 0;
	private ViewSize size = new ViewSize(0,0);
	private String dataSetID;
	private String dataSetColID;
	private String horizontalAlign;
	private String verticalAlign;
	private DataSetComponent dataSetComponent;
	private EFDataSet dataSet;
	private EFRowSet mainRowSet;

	public int getPercentWidth() {
		return percentWidth;
	}

	public void setId(String id) {
		this.id = id;
	}

	public float getX() {
		return x;
	}

	public void setX(float x) {
		this.x = x;
	}

	public float getY() {
		return y;
	}

	public void setY(int y) {
		this.y = y;
	}

	public Boolean getVisible() {
		return visible;
	}

	public void setVisible(Boolean visible) {
		this.visible = visible;
	}

	public int getTabIndex() {
		return tabIndex;
	}

	public void setTabIndex(int tabIndex) {
		if(tabIndex != -1) {
			this.tabIndex = tabIndex;
		}
	}

	public Boolean getEditable() {
		return editable;
	}

	public void setEditable(Boolean editable) {
		this.editable = editable;
	}

	public String getLayoutType() {
		return layoutType;
	}

	public void setLayoutType(String layoutType) {
		this.layoutType = layoutType;
	}

	public int getRequestColCount() {
		return requestColCount;
	}

	public void setRequestColCount(int requestColCount) {
		this.requestColCount = requestColCount;
	}

	public double getHorizontalGap() {
		return horizontalGap;
	}

	public void setHorizontalGap(double horizontalGap) {
		this.horizontalGap = horizontalGap;
	}

	public double getVerticalGap() {
		return verticalGap;
	}

	public void setVerticalGap(double verticalGap) {
		this.verticalGap = verticalGap;
	}

	public int getFontColor() {
		return fontColor;
	}

	public void setFontColor(int fontColor) {
		this.fontColor = fontColor;
	}

	public int getFontSize() {
		return fontSize;
	}

	public void setFontSize(int fontSize) {
		this.fontSize = fontSize;
	}

	@Override
	public IUIComponent getParentComp() {
		return parentComp;
	}

	@Override
	public void setParentComp(IUIComponent comp) {
		parentComp = comp;
	}
}